package engine.web;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import engine.content.ContentTypes;
import engine.content.IContent;
import engine.dataobjects.MailItem;
import engine.dataobjects.MailItemInstance;
import engine.services.IContentService;
import engine.services.IDataResource;
import engine.services.IMailService;

@Controller
@RequestMapping("/items")
public class ContentController {

	@Autowired
	IContentService contentService;
	@Autowired
	IDataResource configResource;
	@Autowired 
	IMailService mailService;
       
    public void setMailService(IMailService mailService) {
		this.mailService = mailService;
	}

	public void setConfigResource(IDataResource configResource) {
		this.configResource = configResource;
	}

	public void setContentService(IContentService contentService) {
		this.contentService = contentService;
	}
	
	@RequestMapping(value="/", method=RequestMethod.GET)
	public @ResponseBody List<MailItem> getItems() throws IOException {
		try {
			this.configResource.refresh();		
			return this.configResource.getAllMailItems();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	
	@RequestMapping(value="/mail/{id}", method=RequestMethod.GET)
	public void mailContent(@RequestParam(value="email",required=true) String emailAddress, @PathVariable(value = "id") String id, HttpServletResponse response) throws IOException {
		try {
			this.configResource.refresh();			
			MailItem item = getContentItem(id);
			MailItemInstance mi = new MailItemInstance(item);
			List<String> email = Arrays.asList(new String[] {emailAddress});
			mi.setTo(email );
			mailService.mail(mi);
			response.setContentType("text/plain");
			response.getWriter().write("sent");
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	

	
	@SuppressWarnings("rawtypes")
	@RequestMapping(value="/html/{id}", method=RequestMethod.GET)
	public void getMailContent(@PathVariable(value = "id") String id, HttpServletResponse response) throws IOException {
		try {
			this.configResource.refresh();
			MailItem contentItem = getContentItem(id);
			IContent content = contentService.getResponse(contentItem);
			response.setContentType(content.getContentType());
			
			if (content.getType() == ContentTypes.FILE || content.getType() == ContentTypes.MEMORY) {
				OutputStream out = response.getOutputStream();
				out.write((byte[])content.getContent());
			} else {
				response.getWriter().write(content.getContentAsString());							
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	private MailItem getContentItem(String id) {
		List<MailItem> items = configResource.getAllMailItems();
		MailItem contentItem = null;
		if (id != null && !id.equals("")) {
			for (MailItem item : items) {
				if (item.getId().equals(id)) {
					contentItem = item;
				}
			}
			if (contentItem == null) {
				int index = Integer.valueOf(id);
				contentItem = items.get(index);					
			}
		}
		if (contentItem == null) {
			throw new RuntimeException("Content was not found");
		}
		return contentItem;
	}
}
